home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 3d_lib.zip / TEST.C < prev    next >
Text File  |  1993-05-09  |  8KB  |  288 lines

  1. #include <3d.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <float.h>
  6. #include <graphics.h>
  7. #include <dos.h>
  8. #include <conio.h>
  9. #include <stdlib.h>
  10.  
  11. void setup();
  12. int huge alwayszero(void);
  13. void _Cdecl vga256_driver(void);
  14.  
  15. int huge alwayszero(void)
  16. {
  17.     return 0;
  18. }
  19.  
  20. void setup()
  21. {
  22.  
  23.    int gdriver,gmode;
  24.    gdriver = installuserdriver("vga256", alwayszero);
  25.    registerbgidriver(vga256_driver);
  26.    initgraph(&gdriver,&gmode,"");
  27.  
  28. }
  29.  
  30. void main()
  31. {
  32.  
  33.     FACE *f,*g,*h,*i,*j,*k;                /* Faces of cube */
  34.     FACE *t1,*t2,*t3,*t4;                  /* Faces of tetrahedron */
  35.     FACE *o1,*o2,*o3,*o4,*o5,*o6,*o7,*o8;  /* Faces of octahedron */
  36.     VECTOR n,s;
  37.     MATRIX xm,ym,xr,yr,om,or,id;           /* Transformation matrices */
  38.     OBJECT *o;                             /* Cube */
  39.     OBJECT *p;                             /* Tetrahedron */
  40.     OBJECT *q;                             /* Octahedron */
  41.  
  42.     int apage,vpage,tpage,frame;
  43.  
  44.     /* Initialize matrices and data structures */
  45.  
  46.     identity (xm);
  47.     identity (ym);
  48.     identity (xr);
  49.     identity (yr);
  50.     identity (id);
  51.     identity (om);
  52.     identity (or);
  53.  
  54.     f = (FACE *)malloc(sizeof(FACE));
  55.     g = (FACE *)malloc(sizeof(FACE));
  56.     h = (FACE *)malloc(sizeof(FACE));
  57.     i = (FACE *)malloc(sizeof(FACE));
  58.     j = (FACE *)malloc(sizeof(FACE));
  59.     k = (FACE *)malloc(sizeof(FACE));
  60.  
  61.     t1 = (FACE *)malloc(sizeof(FACE));
  62.     t2 = (FACE *)malloc(sizeof(FACE));
  63.     t3 = (FACE *)malloc(sizeof(FACE));
  64.     t4 = (FACE *)malloc(sizeof(FACE));
  65.  
  66.     o1 = (FACE *)malloc(sizeof(FACE));
  67.     o2 = (FACE *)malloc(sizeof(FACE));
  68.     o3 = (FACE *)malloc(sizeof(FACE));
  69.     o4 = (FACE *)malloc(sizeof(FACE));
  70.     o5 = (FACE *)malloc(sizeof(FACE));
  71.     o6 = (FACE *)malloc(sizeof(FACE));
  72.     o7 = (FACE *)malloc(sizeof(FACE));
  73.     o8 = (FACE *)malloc(sizeof(FACE));
  74.  
  75.     o = (OBJECT *)malloc(sizeof(OBJECT));
  76.     new_obj (o);
  77.  
  78.     p = (OBJECT *)malloc(sizeof(OBJECT));
  79.     new_obj (p);
  80.  
  81.     q = (OBJECT *)malloc(sizeof(OBJECT));
  82.     new_obj (q);
  83.  
  84.     /* Define cube */
  85.  
  86.     new_face (f);
  87.     add_corner (   0.0,   0.0,   0.0,f);
  88.     add_corner ( 25.0,   0.0,   0.0,f);
  89.     add_corner ( 25.0, 25.0,   0.0,f);
  90.     add_corner (   0.0, 25.0,   0.0,f);
  91.     add_face (o,f);
  92.  
  93.     new_face (g);
  94.     add_corner (   0.0,   0.0,   0.0,g);
  95.     add_corner (   0.0, 25.0,   0.0,g);
  96.     add_corner (   0.0, 25.0, 25.0,g);
  97.     add_corner (   0.0,   0.0, 25.0,g);
  98.     add_face (o,g);
  99.  
  100.     new_face (h);
  101.     add_corner (   0.0,   0.0,   0.0,h);
  102.     add_corner (   0.0,   0.0, 25.0,h);
  103.     add_corner ( 25.0,   0.0, 25.0,h);
  104.     add_corner ( 25.0,   0.0,   0.0,h);
  105.     add_face (o,h);
  106.  
  107.     new_face (i);
  108.     add_corner (   0.0,   0.0, 25.0,i);
  109.     add_corner (   0.0, 25.0, 25.0,i);
  110.     add_corner ( 25.0, 25.0, 25.0,i);
  111.     add_corner ( 25.0,   0.0, 25.0,i);
  112.     add_face (o,i);
  113.  
  114.     new_face (j);
  115.     add_corner (   0.0, 25.0,   0.0,j);
  116.     add_corner ( 25.0, 25.0,   0.0,j);
  117.     add_corner ( 25.0, 25.0, 25.0,j);
  118.     add_corner (   0.0, 25.0, 25.0,j);
  119.     add_face (o,j);
  120.  
  121.     new_face (k);
  122.     add_corner ( 25.0,   0.0,   0.0,k);
  123.     add_corner ( 25.0,   0.0, 25.0,k);
  124.     add_corner ( 25.0, 25.0, 25.0,k);
  125.     add_corner ( 25.0, 25.0,   0.0,k);
  126.     add_face (o,k);
  127.  
  128.     /* Define tetrahedron */
  129.  
  130.     new_face (t1);
  131.     add_corner (   0.00,   0.00,   0.00,t1);
  132.     add_corner ( 100.00,   0.00,   0.00,t1);
  133.     add_corner (  50.00,  81.65,  28.87,t1);
  134.     add_face (p,t1);
  135.  
  136.     new_face (t2);
  137.     add_corner (   0.00,   0.00,   0.00,t2);
  138.     add_corner (  50.00,   0.00,  86.60,t2);
  139.     add_corner ( 100.00,   0.00,   0.00,t2);
  140.     add_face (p,t2);
  141.  
  142.     new_face (t3);
  143.     add_corner (   0.00,   0.00,   0.00,t3);
  144.     add_corner (  50.00,  81.65,  28.87,t3);
  145.     add_corner (  50.00,   0.00,  86.60,t3);
  146.     add_face (p,t3);
  147.  
  148.     new_face (t4);
  149.     add_corner ( 100.00,   0.00,   0.00,t4);
  150.     add_corner (  50.00,   0.00,  86.60,t4);
  151.     add_corner (  50.00,  81.65,  28.87,t4);
  152.     add_face (p,t4);
  153.  
  154.     /* Define octahedron */
  155.  
  156.     new_face (o1);
  157.     add_corner (   0.00,  70.71,   0.00,o1);
  158.     add_corner (  50.00,   0.00,  50.00,o1);
  159.     add_corner ( 100.00,  70.71,   0.00,o1);
  160.     add_face (q,o1);
  161.  
  162.     new_face (o2);
  163.     add_corner ( 100.00,  70.71,   0.00,o2);
  164.     add_corner (  50.00,   0.00,  50.00,o2);
  165.     add_corner ( 100.00,  70.71, 100.00,o2);
  166.     add_face (q,o2);
  167.  
  168.     new_face (o3);
  169.     add_corner ( 100.00,  70.71, 100.00,o3);
  170.     add_corner (  50.00,   0.00,  50.00,o3);
  171.     add_corner (   0.00,  70.71, 100.00,o3);
  172.     add_face (q,o3);
  173.  
  174.     new_face (o4);
  175.     add_corner (   0.00,  70.71, 100.00,o4);
  176.     add_corner (  50.00,   0.00,  50.00,o4);
  177.     add_corner (   0.00,  70.71,   0.00,o4);
  178.     add_face (q,o4);
  179.  
  180.     new_face (o5);
  181.     add_corner (   0.00,  70.71,   0.00,o5);
  182.     add_corner (  50.00, 141.40,  50.00,o5);
  183.     add_corner (   0.00,  70.71, 100.00,o5);
  184.     add_face (q,o5);
  185.  
  186.     new_face (o6);
  187.     add_corner (   0.00,  70.71, 100.00,o6);
  188.     add_corner (  50.00, 141.40,  50.00,o6);
  189.     add_corner ( 100.00,  70.71, 100.00,o6);
  190.     add_face (q,o6);
  191.  
  192.     new_face (o7);
  193.     add_corner ( 100.00,  70.71, 100.00,o7);
  194.     add_corner (  50.00, 141.40,  50.00,o7);
  195.     add_corner ( 100.00,  70.71,   0.00,o7);
  196.     add_face (q,o7);
  197.  
  198.     new_face (o8);
  199.     add_corner ( 100.00,  70.71,   0.00,o8);
  200.     add_corner (  50.00, 141.40,  50.00,o8);
  201.     add_corner (   0.00,  70.71,   0.00,o8);
  202.     add_face (q,o8);
  203.  
  204.     /* The light source in the z direction (from the eye) */
  205.  
  206.     s[0] =  0.25;
  207.     s[1] =  0.0;
  208.     s[2] =  1.0;
  209.  
  210.     /* Center figures in space and orient them */
  211.  
  212.     /* The following series of function calls illustrates the concatenation
  213.        of 3D transforms.  Each of three matrices xm, ym, and om is the
  214.        concatenation of four transforms; first, center the object on the
  215.        origin, second, rotate the object about the y axis, third, rotate the
  216.        object about the x axis, and fourth, translate the object to its final
  217.        position.  All four transforms are concatenated in each matrix, then
  218.        the object is transformed.  Thus, each vertex in the object is mul-
  219.        tiplied by the transformation matrix only once.    */
  220.  
  221.     trans (-10,-10,-10,xm);
  222.     trans (-10,-10,-10,ym);
  223.     trans (-10,-10,-10,om);
  224.     yrot (M_PI/4,xm);
  225.     yrot (M_PI/3,ym);
  226.     yrot (M_PI/4,om);
  227.     xrot (M_PI/4,xm);
  228.     xrot (M_PI/3,ym);
  229.     xrot (M_PI/4,om);
  230.  
  231.     /* Position figures in space */
  232.  
  233.     trans (100,100,100,xm);
  234.     trans (200,50,100,ym);
  235.     trans (200,150,100,om);
  236.     xform (*o,xm);
  237.  
  238.     xform (*p,ym);
  239.     xform (*q,om);
  240.  
  241.     /* Set up rotation matrices */
  242.  
  243.     /* These matrices are created to perform an incremental rotation of each
  244.        object.  First, the object is translated to the origin, second, the
  245.        object is rotated, and third, the object is translated back to its
  246.        original postion. */
  247.  
  248.     trans (-100,-100,-100,xr);
  249.     yrot (M_PI/45,xr);
  250.     trans (100,100,100,xr);
  251.  
  252.     trans (-200,-50,-100,yr);
  253.     xrot (M_PI/45,yr);
  254.     trans (200,100,100,yr);
  255.  
  256.     trans (-200,-200,-100,or);
  257.     xrot (M_PI/45,or);
  258.     yrot (M_PI/45,or);
  259.     trans (200,150,100,or);
  260.  
  261.     apage = 0;
  262.     vpage = 1;
  263.  
  264. setup();
  265.  
  266.     for (frame = 1; frame <= 45; frame++)
  267.     {
  268.         setvisualpage(vpage);
  269.         setactivepage(apage);
  270.         tpage = apage;
  271.         apage = vpage;
  272.         vpage = tpage;
  273.         cleardevice();
  274.         xform (*o,xr);
  275.         xform (*p,yr);
  276.         xform (*q,or);
  277.  
  278.         disp_object (s,1,o,id);
  279.         disp_object (s,4,p,id);
  280.         disp_object (s,2,q,id);
  281.     }
  282.  
  283. closegraph();
  284.  
  285. }
  286.  
  287.  
  288.